package com.gaohe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaohe.common.R;
import com.gaohe.pojo.*;
import com.gaohe.service.CategoryService;
import com.gaohe.service.DishService;
import com.gaohe.service.SetMealDishService;
import com.gaohe.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetMealDishService setMealDishService;
    @Autowired
    private DishService dishService;

    //分页查询
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> setmealDtoPage = setMealService.PAGE_R(page, pageSize, name);
        return R.success(setmealDtoPage);
    }

    //新增数据
    @PostMapping
    public R<String> addSetMeal(@RequestBody SetmealDto setmealDto) {
        setMealService.insertSetMeal(setmealDto);
        return R.success("数据添加成功");
    }

    //修改套餐数据的数据回显
    @GetMapping("/{id}")
    public R<SetmealDto> updateSetMeal1(@PathVariable("id") Long id) {
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = setMealService.getById(id);
        BeanUtils.copyProperties(setmeal, setmealDto);
        //得到categoryName并加入到setmealDto中
        Long categoryId = setmeal.getCategoryId();
        Category category = categoryService.getById(categoryId);
        String categoryName = category.getName();
        setmealDto.setCategoryName(categoryName);
        //加入setMealDish
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper();
        lqw.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> setmealDishes = setMealDishService.list(lqw);
        setmealDto.setSetmealDishes(setmealDishes);
        return R.success(setmealDto);
    }

    //修改数据
    @PutMapping
    private R<String> updateSetMeal2(@RequestBody SetmealDto setmealDto) {
        setMealService.updateSetMeal_Dish(setmealDto);
        return R.success("修改数据成功");
    }

    //停售
    @PostMapping("/status/{id}")
    public R stopSales(@PathVariable Integer id, String ids) {
        setMealService.stop(id, ids);
        return R.success("修改成功");
    }

    //删除数据
    @DeleteMapping
    private R<String> deleteSetMeal(String ids) {
        setMealService.deleteSetMealDish(ids);
        return R.success("删除成功");
    }

    //移动端套餐种类的套餐展示展示
    @GetMapping("/list")
    public R<List<Setmeal>> setMealList(Long categoryId, Integer status) {
//        log.info("{},{}", categoryId, status);
        //根据categoryId查询套餐种类对应的套餐
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper();
        lqw.eq(Setmeal::getCategoryId, categoryId);
        List<Setmeal> setmealList = setMealService.list(lqw);
        return R.success(setmealList);
    }

    //套餐的菜品展示
    @GetMapping("/dish/{id}")
    public R<List<SetmealDish>> dishList(@PathVariable("id") Long id) {
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setMealDishService.list(lqw);
        //为setmealDishList遍历插入从dish表得来的image地址，并返回到前端
        setmealDishList.stream().map(setmealDish -> {
            Long dishId = setmealDish.getDishId();
            Dish dish = dishService.getById(dishId);
            setmealDish.setImage(dish.getImage());
            return setmealDish;
        }).collect(Collectors.toList());
        return R.success(setmealDishList);

    }

}
